﻿using B.S.ProcessEgine.API.Applocations.Commands.FormNodes;
using B.S.ProcessEgine.API.Dtos.ResponseDto;
using B.S.ProcessEgine.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace B.S.ProcessEgine.API.Controllers
{

    /// <summary>
    /// 表单节点管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FormNodeController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<FormNodeController> logger;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public FormNodeController(IMediator mediator, ILogger<FormNodeController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 表单节点新增
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddFromNode(AddFormNodeCommand command)
        {
            try
            {
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("添加表单节点出错" + ex.Message);
                return ApiResult.Fail("添加表单节点异常", StateCodeEnum.Exception);
            }
        }
        /// <summary>
        /// 表单节点列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<FormNodeResponseDto>>> GetFromNode([FromQuery] GetFormNodeCommand command)
        {
            try
            {
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("添加表单节点出错" + ex.Message);
                //return ApiResult<ApiPaging<FormNodeResponseDto>>.Fail("添加表单节点异常", StateCodeEnum.Exception);
                throw;
            }
        }

        /// <summary>
        /// 表单节点删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult> DeleteFromNode(DeleteFormNodeCommand command)
        {
            try
            {
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("删除表单节点出错" + ex.Message);
                return ApiResult.Fail("删除表单节点异常", StateCodeEnum.Exception);
            }
        }

        /// <summary>
        /// 表单节点修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> UpdateFromNode(UpdFormNodeCommand command)
        {
            try
            {
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("修改表单节点出错" + ex.Message);
                return ApiResult.Fail("修改表单节点异常", StateCodeEnum.Exception);
            }
        }
        /// <summary>
        /// 保存表单节点
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> SaveFormNodeDetails(SaveFormNodesCommand command)
        {
            try
            {
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("保存表单节点出错" + ex.Message);
                return ApiResult.Fail("保存表单节点异常", StateCodeEnum.Exception);
            }
        }

    }
}
